<?php
/**
 *  💡 免责声明：本软件不得用于商业用途，仅做学习交流
 *  ⚠️ 权利声明：本软件由版权人提供产品技术支持
 *  📅 项目编号：NX2025-0135
 */
namespace App\Utils;

class Response
{
    protected $message = [
        1 => [
            'zh_cn' => '成功',
            'en' => 'Success',
        ],
        2 => [
            'zh_cn' => '未知错误',
            'en' => 'Unknown error',
        ],
        3 => [
            'zh_cn' => '服务暂时不可用',
            'en' => 'Service temporarily unavailable',
        ],
        4 => [
            'zh_cn' => '不支持的接口方法',
            'en' => 'Unsupported openapi method api',
        ],
        5 => [
            'zh_cn' => '已达到请求限制',
            'en' => 'Api request limit reached',
        ],
        6 => [
            'zh_cn' => '未经授权的客户端IP地址',
            'en' => 'Unauthorized client IP address:%s open api',
        ],
        7 => [
            'zh_cn' => '没有相关数据',
            'en' => 'No relevant data',
        ],
        10 => [
            'zh_cn' => '参数以下发',
            'en' => 'Parameter to deliver',
        ],
        100 => [
            'zh_cn' => '无效的参数',
            'en' => 'Invalid parameter',
        ],
        101 => [
            'zh_cn' => '令牌无效',
            'en' => 'Invalid token',
        ],
        102 => [
            'zh_cn' => '令牌密钥无效',
            'en' => 'Invalid token Api key',
        ],
        103 => [
            'zh_cn' => '无效的用户id',
            'en' => 'Invalid call_id parameter Call_id',
        ],
        104 => [
            'zh_cn' => '签名不正确',
            'en' => 'Incorrect signature',
        ],
        105 => [
            'zh_cn' => '参数太多',
            'en' => 'Too many parameters',
        ],
        106 => [
            'zh_cn' => '不支持的签名方法',
            'en' => 'Unsupported signature method',
        ],
        200 => [
            'zh_cn' => '没有访问数据的权限',
            'en' => 'No permission to access data',
        ],
        900 => [
            'zh_cn' => '不存在这样的应用程序',
            'en' => 'No such application exists',
        ],
        1001 => [
            'zh_cn' => '更新失败',
            'en' => 'Update Failed',
        ],
        1002 => [
            'zh_cn' => '删除失败',
            'en' => 'Delete Failed',
        ],
        1003 => [
            'zh_cn' => '添加失败',
            'en' => 'Add Failed',
        ],
        1004 => [
            'zh_cn' => '操作失败',
            'en' => 'Operate Failed',
        ],
        1005 => [
            'zh_cn' => '接口请求过多',
            'en' => 'Interface request too much',
        ],
        2001 => [
            'zh_cn' => '用户名或密码错误',
            'en' => 'User name or password error',
        ],
        2002 => [
            'zh_cn' => '用户不存在',
            'en' => 'The user does not exist',
        ],
        2003 => [
            'zh_cn' => '手机号码为空',
            'en' => 'Mobile phone number is empty',
        ],
        2004 => [
            'zh_cn' => '手机发送时操作参数为空',
            'en' => 'The operate parameters are empty when the mobile phone is sent',
        ],
        2005 => [
            'zh_cn' => '注册的验证码已过期',
            'en' => 'Registered verification code has expired',
        ],
        2006 => [
            'zh_cn' => '验证码错误',
            'en' => 'Verification code error',
        ],
        2007 => [
            'zh_cn' => '密码为空',
            'en' => 'Password is empty',
        ],
        2008 => [
            'zh_cn' => '这个账户名已经存在',
            'en' => 'The user name already exists',
        ],
        2009 => [
            'zh_cn' => '用户注册失败',
            'en' => 'User registration failure',
        ],
        2010 => [
            'zh_cn' => '发送消息失败',
            'en' => 'Send Message Failed',
        ],
        2011 => [
            'zh_cn' => '未设置支付密码',
            'en' => 'No payment password set',
        ],
        2012 => [
            'zh_cn' => '支付密码错误',
            'en' => 'payment password error',
        ],
        2013 => [
            'zh_cn' => '超过最大现金次数',
            'en' => 'Exceeding the maximum number of cash in times',
        ],
        2014 => [
            'zh_cn' => '余额不够',
            'en' => 'balance is not enough',
        ],
        2015 => [
            'zh_cn' => '冻结用户',
            'en' => 'Frozen User',
        ],
        9527 => [
            'zh_cn' => '自定义错误',
            'en' => 'Custom Error',
        ],
        12001 => [
            'zh_cn' => '参数格式错误',
            'en' => 'Parameters format error',
        ],
        12002 => [
            'zh_cn' => '订单失败',
            'en' => 'ORDER FAILED',
        ],
        500 => [
            'zh_cn' => '内部服务错误',
            'en' => 'Internal Server Error',
        ],
    ];

    protected $language = 'zh_cn';

    protected $token;

    /**
     * @param mixed $token
     */
    public function setToken($token): void
    {
        $this->token = $token;
    }

    public function response($data = [], $code = 1, $message = null, $language = null)
    {
        $response = [
            'code' => $code,
            'message' => $message ?: $this->message[$code][$language ?: $this->language],
            'data' => $data,
            'time' => now()->getTimestampMs(),
        ];
        if ($this->token) {
            $response['token'] = $this->token;
        }
        //TODO 验签
        if ($code == 500) {
            return response($response, 500, ['Access-control-allow-origin' => '*']);
        }
        return response($response, 200, ['Access-control-allow-origin' => '*']);
    }

    protected function sign()
    {
    }
}
